// -----------------------------------------------------------------------
//  <copyright file="StartupModule.cs" company="LiuliuSoft">
//      Copyright (c) 2022-2025 DaprPlus. All rights reserved.
//  </copyright>
//  <site>https://dapr.plus</site>
//  <last-editor>郭明锋</last-editor>
//  <last-date>2025/1/15 1:26</last-date>
// -----------------------------------------------------------------------

using DaprPlus.AspNetCore.Middleware;
using DaprPlus.Identity.Api.Startups;
using DaprPlus.Identity.Application;
using DaprPlus.Identity.Infrastructure.Authorization;
using DaprPlus.Identity.Infrastructure.AutoMapper;
using DaprPlus.Identity.Infrastructure.Identity;

using FluentValidation;

using Shares.Infrastructure.AspNetCore;
using Shares.Infrastructure.Logging;
using Shares.Infrastructure.Redis;


namespace DaprPlus.Identity.Api;

[Description("IdentityApi启动模块")]
[DependsOnModules(
    typeof(DaprAspNetCoreModule)
    , typeof(RedisModule)
    , typeof(LoggingModule)
    , typeof(AuthorizationModule)
    , typeof(OpenApiModule)
    , typeof(IdentityAutoMapperModule)
    , typeof(IdentityModule)
    , typeof(IdentityAppModule)
)]
public class StartupModule : AspModuleBase
{
    /// <summary>
    /// 将模块服务添加到依赖注入服务容器中
    /// </summary>
    /// <param name="services">依赖注入服务容器</param>
    /// <returns></returns>
    public override IServiceCollection AddServices(IServiceCollection services)
    {
        services.AddLocalization(opts => opts.ResourcesPath = "Properties");
        services.AddRequestLocalization(opts =>
        {
            var cultures = new[] { "zh-Hans", "zh-Hant", "en" };
            opts.SetDefaultCulture(cultures[0])
                .AddSupportedCultures(cultures)
                .AddSupportedUICultures(cultures);
        });

        services.AddValidatorsFromAssembly(Assembly);
        services.AddHostedService<Worker>();

        return base.AddServices(services);
    }

    /// <summary>
    /// 应用AspNetCore的服务业务
    /// </summary>
    /// <param name="app">Web应用程序</param>
    public override async Task UseModule(WebApplication app)
    {
        app.UseRequestLocalization();

        if (!app.Environment.IsDevelopment())
        {
            app.UseExceptionHandler("/Error", true);
            app.UseHsts();
        }

        app.UseStaticFiles().UseAntiforgery();
        app.UseMiddleware<CultureMiddleware>();

        await base.UseModule(app);
    }
}
